package com.pubinfo.passbook.api.controller.contract;

import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.common.entity.TContractBase;
import com.pubinfo.passbook.common.model.dto.contract.ContractAdoptParam;
import com.pubinfo.passbook.common.model.dto.contract.ContractIdParam;
import com.pubinfo.passbook.common.model.dto.contract.ContractListParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.model.vo.base.ResultCode;
import com.pubinfo.passbook.common.model.vo.contract.ContractDetailInfoVo;
import com.pubinfo.passbook.common.model.vo.contract.ContractDetailVo;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api(tags = "QGPB-合同管理")
@RequestMapping(value = "contract")
public class ContractBaseController {
    
    @Resource
    private UserInfoService userInfoService;
    
    @Resource
    private TContractBaseService contractBaseService;
    
    @PostMapping("list")
    @ApiOperation(value = "查询合同列表数据02")
    public JsonResult<MyPage<TContractBase>> list(@RequestBody ContractListParam param) {
        UserInfo userInfo = userInfoService.getCurrentUserInfo();
        param.setRoleCode(userInfo.getRoleCode());
        param.setAcct(userInfoService.getCurrentAcct());
        param.setOrgId(userInfo.getOrgId());
        
        // param.setContractType(ContractTypeEnum.FORMAL);
        return JsonResult.success(new MyPage(contractBaseService.selectContractList(param)));
    }

    @PostMapping("info")
    @ApiOperation(value = "合同信息")
    public JsonResult<ContractDetailInfoVo> info(@RequestBody ContractIdParam param) {
        return JsonResult.success(contractBaseService.contractInfo(param));
    }

    @PostMapping("detail")
    @ApiOperation(value = "合同详情")
    public JsonResult<ContractDetailVo> detail(@RequestBody ContractIdParam param) {
        return JsonResult.success(contractBaseService.contractDetail(param));
    }
    
    @PostMapping("adopt")
    @ApiOperation(value = "合同认领")
    public JsonResult<ResultCode> adopt(@RequestBody ContractAdoptParam param) {
        UserInfo userInfo = userInfoService.getCurrentUserInfo();
        param.setRoleCode(userInfo.getRoleCode());
        return contractBaseService.contractAdopt(userInfoService.getCurrentAcct(), param);
    }
    
    @PostMapping("cancelAdopt")
    @ApiOperation(value = "取消认领")
    public JsonResult<ResultCode> cancelAdopt(@RequestBody ContractIdParam param) {
        return contractBaseService.cancelAdopt(userInfoService.getCurrentAcct(), param);
    }
    
    @PostMapping("close")
    @ApiOperation(value = "合同关闭|恢复，当状态为非关闭时调用即关闭，如果是关闭状态时调用即设置未认领")
    public JsonResult<ResultCode> close(@RequestBody ContractIdParam param) {
        return contractBaseService.contractClose(userInfoService.getCurrentAcct(), param);
    }

}
